﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STP;

namespace Simulator
{
	public class PortTree
	{
		readonly Port _port;
		readonly uint _treeIndex;

		public PortTree (Port port, uint treeIndex)
		{
			_port = port;
			_treeIndex = treeIndex;
		}

		[Description ("The role variable described in §13.25.51 in 802.1Q-2011")]
		public PortRole role
		{
			get { return _port.Bridge.Wrapper.GetPortRole (_port.PortIndex, _treeIndex); }
		}

		[Description ("The learning variable described in §13.25.22 in 802.1Q-2011")]
		public bool learning
		{
			get { return _port.Bridge.Wrapper.GetPortLearning (_port.PortIndex, _treeIndex); }
		}

		[Description ("The forwarding variable described in §13.25.15 in 802.1Q-2011")]
		public bool forwarding
		{
			get { return _port.Bridge.Wrapper.GetPortForwarding (_port.PortIndex, _treeIndex); }
		}
		
		[TypeConverter (typeof (UInt8HexTypeConverter))]
		[DefaultValueAttribute ((object) (byte) 0x80)]
		[Description ("The settable priority component (most significant 4 bits) of the portId variable described in §13.25.32. "
			+ "Possible values are 0..0xF0 in steps of 0x10. Default is 0x80.")]
		public byte Priority
		{
			get { return _port.Bridge.Wrapper.GetPortPriority (_port.PortIndex, _treeIndex); }

			set 
			{
				lock (_port.Bridge.WrapperLock)
					_port.Bridge.Wrapper.SetPortPriority (_port.PortIndex, _treeIndex, value, (uint) System.Environment.TickCount); 
			}
		}

		[Description ("The tree index corresponding to the VLAN currently selected in the user interface. "
		+ "Tree index 0 is the CIST, 1-64 are the respective MSTIs.")]
		public uint TreeIndex
		{
			get { return _treeIndex; }
		}

		[TypeConverter (typeof (UInt16HexTypeConverter))]
		[Description ("13.25.32 in 802.1Q-2011")]
		public ushort portId
		{
			get { return _port.Bridge.Wrapper.GetPortIdentifier (_port.PortIndex, _treeIndex); }
		}
	}
}
